<?php
class FilmsController extends AppController {

	var $name = 'Films';

	function index() {
		$films  = $this->Film->find('all',array('order'=>array('Product.score DESC') ));
		$this->set('films',$films);
	}
	function adminindex() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Film->recursive = 0;
			$this->set('films', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	function adminview($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid film', true));
				$this->redirect(array('action' => 'index'));
			}
			$this->loadModel('Product');
			$this->loadModel('Comment');
			//$this->set('film', $this->Film->read(null, $id));
			$film = $this->Film->read(null, $id);
			$film += $this->Product->read(null,$id);
			$comment = $this->Comment->find('all',array('conditions'=>array('product_id'=>$film['Product']['id']),'fields'=>array('User.username','User.id','Comment.*')));
			$film['Comment'] = $comment;
			$this->set(compact('film'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid film', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->loadModel('Product');
		$this->loadModel('Comment');
		//$this->set('film', $this->Film->read(null, $id));
		$film = $this->Film->read(null, $id);
		$film += $this->Product->read(null,$id);
		$comment = $this->Comment->find('all',array('conditions'=>array('product_id'=>$film['Product']['id']),'fields'=>array('User.username','User.id','Comment.*')));
		$film['Comment'] = $comment;
		$genre2 = $film['Product']['genre'];
		//$this->Book->contain(array('Book','Book.Producer'));
        //$related_book = "select * from products as Product,producers as Producer,books as Book where $genre = Product.genre order by Product.sold_count desc limit 3";
        $related_film = $this->Film->find("all",array('conditions'=>array('Product.genre'=>$genre2,'Film.id <>'=>$film['Product']['id']),'order'=>'Product.sold_count desc','limit' => 3));
        //$result = $this->Product->query($related_book);
        $other_films = $this->Film->find("all",array('conditions'=>array('Film.producer_id'=>$film['Film']['producer_id'],'Film.id <>'=>$film['Product']['id']),'order'=>'Product.sold_count desc','limit' => 3));
	    $this->set(compact('film','related_film','other_films'));
	}

	function add() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->loadModel('Product');
			if (!empty($this->data)) {
				$this->setDefault($this->data);
				$this->Product->create();
				if ($this->Product->save($this->data)) {
					$this->data['Film']['id'] = $this->Product->id;
					$this->Film->create();
					if ($this->Film->save($this->data)) {
						$this->Session->setFlash(__('The film has been saved', true));
						//$this->redirect(array('action' => 'index'));
					} else {
						$this->Session->setFlash(__('The film could not be saved. Please, try again.', true));
					}
					} else {
					$this->Session->setFlash(__('The film could not be saved. Please, try again.', true));
				}
			}
			$producers = $this->Film->Producer->find('list');
			$this->set(compact('producers'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->loadModel('Product');
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid film', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				$this->setDefault($this->data);
				if ($this->Product->save($this->data)) {
					$this->data['Film']['id'] = $this->Product->id;
						$this->Film->create();
						if ($this->Film->save($this->data)) {
							$this->Session->setFlash(__('The film has been saved', true));
							$this->redirect(array('action' => 'index'));
						} else {
							$this->Session->setFlash(__('The film could not be saved. Please, try again.', true));
						}
					//$this->Session->setFlash(__('The film has been saved', true));
					//$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The film could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Film->read(null, $id);
				$this->data += $this->Product->read(null, $id);
			}
			$producers = $this->Film->Producer->find('list');
			$this->set(compact('producers'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->loadModel('Product');
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for film', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Film->delete($id)) {
				if ($this->Product->delete($id)) {
				$this->Session->setFlash(__('Film deleted', true));
				$this->redirect(array('action'=>'index'));
			}}
			$this->Session->setFlash(__('Film was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	function setDefault(&$data) {
			if($data['Product']['image']=='') {
				$data['Product']['image']='urun-resmi-yok.png';
			}
			
			if($data['Product']['sold_count']=='') {
				$data['Product']['sold_count']='0';
			}
			
			if($data['Product']['stock_count']=='') {
				$data['Product']['stock_count']='0';
			}
	}
	
	function category($genre) {
		// $books=$this->Book->query("select * from books where ID=2");
		$films  = $this->Film->find('all',array('conditions'=>array('Product.genre'=>$genre),'order'=>array('Product.score DESC') ));
		//$books  = $this->Book->find('first',array('conditions'=>array('Book.id'=>'1'),'fields'=>array('Book.id','Product.name')));
		//$books  = $this->Book->find('first' ,array('conditions'=>array('Book.id'=>'1'),'fields'=>array('Book.id','Product.name')));
		//$books = $this->Book->read(array('Product.name','Book.id'),'1');
		
		$this->set('films',$films);
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
}
